$(function(){ //layer统一定义
    window.ybLayer = layui.layer;
    window.ybForm = layui.form;
    window.ybTable = layui.table;
    window.ybLaydate = layui.laydate;
    window.ybLaypage = layui.laypage;

    //禁止后退键 作用于Firefox、Opera
    document.onkeypress = forbidBackSpace;
    //禁止后退键  作用于IE、Chrome
    document.onkeydown = forbidBackSpace;

});

function forbidBackSpace(e) {
    var ev = e || window.event; //获取event对象
    if(ev.keyCode != 8){
        return true;
    }
    var obj = ev.target || ev.srcElement; //获取事件源
    var t = obj.type || obj.getAttribute('type'); //获取事件源类型
    //获取作为判断条件的事件类型
    var vReadOnly = obj.readOnly;
    var vDisabled = obj.disabled;
    //处理undefined值情况
    vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;
    vDisabled = (vDisabled == undefined) ? true : vDisabled;
    //当敲Backspace键时，事件源类型为密码或单行、多行文本的，
    //并且readOnly属性为true或disabled属性为true的，则退格键失效
    var flag1 = (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);
    //当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效
    var flag2 = t != "password" && t != "text" && t != "textarea";
    //判断
    if (flag2 || flag1) return false;
}


function ajaxGet(evt, ctrlView, successCallBackFunc, errorCallBackFunc) {
    (ctrlView == undefined) ? ctrlView = $(this) : false;

    if (ctrlView.hasClass('confirm')) {
        if (!confirm('确认要执行该操作吗?')) {
            ybLayer.msg('操作已取消!');
            return false;
        }
    }

    var target = ctrlView.attr('href') || ctrlView.attr('url');
    if (target !== undefined && target !== '' && target !== '#') {
        $.get(target, function (data) {
            autoTipAndGo(data, successCallBackFunc, errorCallBackFunc);
        });
    }
    return false;
}

function formPost(evt, ctrlView, successCallBackFunc, errorCallBackFunc, beforeSubmiteFunc) {
    (ctrlView == undefined) ? ctrlView = $(this) : false;
    var formId;
    if (formId = ctrlView.attr('formId')) {
        var target = $('#' + formId).attr('action') || ctrlView.attr('href') || ctrlView.attr('url');
        if (target !== undefined && target !== '' && target !== '#') {
            var requestParams = "";
            if (undefined != beforeSubmiteFunc && '' != beforeSubmiteFunc) {
                beforeSubmiteFunc = eval(beforeSubmiteFunc);
                requestParams = beforeSubmiteFunc();
            }
            requestParams += $('#' + formId).find('input, select, textarea').serialize();
            $.post(target, requestParams, function (data) {
                autoTipAndGo(data, successCallBackFunc, errorCallBackFunc);
            });
        }
    }
    return false;
}

function ajaxPost(url, requestParams, successCallBackFunc, errorCallBackFunc) {
    $.post(url, requestParams, function (data) {
        autoTipAndGo(data, successCallBackFunc, errorCallBackFunc);
    });
    return false;
}


function autoTipAndGo(res, successCallBackFunc, errorCallBackFunc) {
    if (res.code == 1) {
        if (undefined != successCallBackFunc && '' != successCallBackFunc) {
            successCallBackFunc = eval(successCallBackFunc);
            successCallBackFunc(res);
        } else {
            if (res.url) {
                ybLayer.msg(res.msg + '<br/><br/>稍后页面将自动跳转~~', {icon: 6});
            } else {
                ybLayer.msg(res.msg, {icon: 6});
            }
            setTimeout(function () {
                if (res.url) {
                    location.href = res.url;
                } else {
                    location.reload(true);
                }
            }, 1000);
        }
    } else {
        if (undefined != errorCallBackFunc && '' != errorCallBackFunc) {
            errorCallBackFunc = eval(errorCallBackFunc);
            errorCallBackFunc(res);
        } else {
            ybLayer.msg(res.msg, {icon: 5});
        }
    }
}

function isArray(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]';
}

function compare(property, ascFlag) {
    return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return ascFlag
            ? (value1 - value2)
            : (value2 - value1);
    }
}


function getRandStr(lenLimit) {
    lenLimit = lenLimit || 8;
    var srcStr = 'abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
    var maxPos = srcStr.length;
    var desStr = '';
    for (var i = 0; i < lenLimit; i++) {
        desStr += srcStr.charAt(Math.floor(Math.random() * maxPos));
    }
    return desStr;
}

function searchParamObj(formId){
    var searchParam = {};
    $('#'+formId).find('input, select, textarea').each(function(){
        if($(this).attr('name') != undefined){
            searchParam[$(this).attr('name')] = $(this).val();
        }
    });
    return searchParam;
}